package cn.com.service.impl.hr;

import static cn.com.support.message.MessageCodeEnum.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.com.commons.Paginator;
import cn.com.dal.dao.hr.DeptDAO;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.service.hr.DeptService;
import cn.com.support.message.MessageResult;

/**
 * 系统设置，部门管理SERVICE实现类
 * @author Zoe
 */
@Service
public class DeptServiceImpl implements DeptService {

	private Logger logger = LoggerFactory.getLogger(DeptServiceImpl.class);

	@Autowired
	private DeptDAO<DeptDO> deptDAO;

	@Override
	public <C> Paginator<C, List<DeptDO>> selectByPage(
			Paginator<C, List<DeptDO>> paginator) {
		Paginator<C, List<DeptDO>> result = null;
		try {
			result = deptDAO.selectForPage(paginator);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]",
							paginator.getCondition()), e);
		}
		//return json string
		return result;
	}

	@Override
	public MessageResult<Long> insert(DeptDO deptDO) {
		MessageResult<Long> result =  new MessageResult<Long>(false, null, ERROR_INSERT);
		try {
			Long id = deptDAO.insert(deptDO);
			if (id != null && id > 0) {
				result = new MessageResult<Long>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			logger.error(String.format(ERROR_INSERT.descript() + ":[%s]", deptDO), e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Integer> updateById(DeptDO deptDO) {
		MessageResult<Integer> result =  new MessageResult<Integer>(false, null, ERROR_UPDATE);
		try {
			Integer id = deptDAO.updateById(deptDO);
			if (id != null && id > 0) {
				result = new MessageResult<Integer>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.error(String.format(ERROR_UPDATE.descript() + ":[%s]", deptDO), e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Integer> deleteById(List<String> idLst) {
		MessageResult<Integer> result =  new MessageResult<Integer>(true, null, INFO_SUCCEED);
		Integer opId = null;
		try {
			for(String id : idLst){
				opId = deptDAO.deleteById(Long.parseLong(id));
				if (opId == null || opId <= 0) {
					result = new MessageResult<Integer>(false, opId, ERROR_DELETE);
					break;
				}
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.error(String.format(ERROR_DELETE.descript() + ":[%s]", idLst), e);
		}
		return result;
	}

	@Override
	public List<DeptDO> selectList(DeptDO deptDO) {
		List<DeptDO> result = null;
		try {
			result = deptDAO.selectList(deptDO);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]"), e);
		}
		return result;
	}

	@Override
	public DeptDO selectById(Long id) {
		DeptDO result = null;
		try {
			result = deptDAO.selectById(id);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]"), e);
		}
		return result;
	}

}
